﻿using System.Diagnostics;
using TMPro;
using UnityEngine;
using UnityEngine.UIElements;
using WorldMapBase;
using Yoozoo.Gameplay.RTS;

public class TrainDefenceBehaviour
{
    public Vector3 position;

    private MarchEntity _march;
    private MarchEntity _defenceMarch;
    private int _battleIdx;
    private float delayTime;
    private int truckAttackerState;
    private float stateStartTime;
    public void Start(MarchEntity defenceMarch, MarchEntity march,int battleIdx,float delayTime)
    {
        truckAttackerState = 1;
        if (delayTime > 0)
        {

            truckAttackerState = 0;
            this.delayTime = delayTime;
        }
        _march = march;
        _defenceMarch = defenceMarch;
        _battleIdx = battleIdx;
        stateStartTime = Time.time;
        UpdatePos();
    }

    public void Update()
    {
        var deltaTime = Time.deltaTime;
        var time = Time.time;
        if (truckAttackerState == 0)
        {
            if (time - stateStartTime >= delayTime)
            {
                truckAttackerState = 1;
                stateStartTime = Time.time;
            }
        }
        if (_march == null || _march.IsReadyToRemove() || !_march.isTrainFighting)
        {
            _defenceMarch?.ReadyToHide();
            UpdatePos();
        }
        else
        {
            UpdatePos();
        }
    }

    public void UpdatePos()
    {
        if (_march != null)
        {
            if (truckAttackerState == 1 || _battleIdx == 1)
            {
                position = _march.Position + _march.Forward * (1 - _battleIdx) * 0.5f;
            }
            else
            {
                position = GlobalValue.INVISIBLE_POSITION;
            }
            Transform tf = _defenceMarch.GetGameObject().transform;
            tf.position = position;
        }
    }
}